define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/order/index' + location.search,
                    add_url: 'user/order/add',
                    edit_url: 'user/order/edit',
                    del_url: 'user/order/del',
                    multi_url: 'user/order/multi',
                    import_url: 'user/order/import',
                    table: 'user_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // fixedColumns: true,
                // fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'lvl.name', title: __('Lvl.name'), operate: 'LIKE'},
                        {field: 'member_day', title: __('Member_day'),operate: false},
                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'order_time', title: __('Order_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'payment_time', title: __('Payment_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4')}, formatter: Table.api.formatter.status},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [ {
                                name: 'agree',
                                title: __('退款'),
                                classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                icon: 'fa fa-check',
                                text: '同意退款',
                                confirm: '确认点击同意退款？',
                                url: 'user/order/agree',
                                visible: function(row) {
                                    // 审核:0=提交资质,1=提交店铺,2=提交审核,3=通过,4=未通过
                                    if (row.status == '2') {
                                        return true;
                                    }
                                },
                                success: function(data, ret) {
                                    table.bootstrapTable('refresh');
                                    return false;
                                },
                                error: function(data, ret) {
                                    console.log(data, ret);
                                    Layer.alert(ret.msg);
                                    return false;
                                }
                            }],
                            formatter: Table.api.formatter.operate,
                        }
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
